﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quality.BasicData;
using System.Data.SqlClient;

namespace Quality.BasicData
{
    public partial class products : Form
    {
      
        public products()
        {
           
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
        }
        private void products_Load(object sender, EventArgs e)
        {
            frm_TransfEvent();

            }
        
        private void 新建toolStripButton1_Click(object sender, EventArgs e)
        {
            string id="";
            productsedit frm = new productsedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); ;
            productsedit frm = new productsedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();

        }

        //事件处理方法
        void frm_TransfEvent()
        {
           SqlDataAdapter da = new SqlDataAdapter();
               da.SelectCommand = new SqlCommand();
            if (keytoolStripTextBox.Text == "")
            {

                da.SelectCommand.CommandText = "select top 1000 a.id,a.num,a.proname,a.specification,a.unit1,b.cusname,c.supname,d.typename,case when a.stop=1 then '停用' end stop from products a left join customers b on a.customer=b.num left join suppliers c on a.supplier=c.num left join  testtypes d on a.testtype=d.num  order by a.id desc";
              
            }
            else
            {

                da.SelectCommand.CommandText = "select a.id,a.num,a.proname,a.specification,a.unit1,b.cusname,c.supname,d.typename,case when a.stop=1 then '停用' end stop from products a left join customers b on a.customer=b.num left join suppliers c on a.supplier=c.num left join  testtypes d on a.testtype=d.num where a.num like '%@key%' or a.proname like '%@key%' order by a.id desc";
                da.SelectCommand.Parameters.AddWithValue("@key", keytoolStripTextBox.Text.Trim());
               
            }
                DataSet dt = DB.getlist(da);
                dataGridView1.DataSource = dt.Tables[0];

        }

        private void 查询toolStripButton1_Click(object sender, EventArgs e)
        {
            frm_TransfEvent();
        }

        private void 删除toolStripButton1_Click(object sender, EventArgs e)
        {
            int count = sqlHelper.Count("select * from pronum where indnum='" + dataGridView1.CurrentRow.Cells[1].Value.ToString() + "'");
            if (count > 0) { MessageBox.Show("成品资料已被使用，不能删除！"); return; }
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "删除", messButton);
            if (dr == DialogResult.OK)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {

                    SqlCommand comm = new SqlCommand();
                    comm.CommandText = "delete from products where id=@id";
                    comm.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
                    DB.dosql(comm);
                    frm_TransfEvent();

                }
            }
        }
      

       
        
    }
}
